package com.fourcitizens.playas

class ProvinceException {
	String message
	Province province
}

class ProvinceService {

    static transactional = true

    Province createProvince(String countryName, String provinceName) {
	 	def province = new Province(name : provinceName)
		def country = Country.findByName(countryName)
		if (country) {
			country.addToProvinces(province)
			
			if (!country.save(failOnError: true))
				throw new ProvinceException(message : "Could not create the province", province)
		}
		
		return province
    }
	
 	Province createProvince(Country country, String provinceName) {
		 return createProvince(country.name, provinceName)
	}
	 
	Province getProvinceInCountry(Country country, String provinceName) {
		
		def province = Province.createCriteria().list {
			and {
				eq('country',country)
				eq('name',provinceName)
			}
		}
		
		return province[0]
	}
	
}
